package com.start.manager.controller;

import com.start.manager.service.IDetailService;
import com.start.manager.util.DateUtil;
import com.start.manager.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jian.li
 * @date 2019年 12月31日 15:09:56
 */
@RestController
@RequestMapping(value = "/home")
public class HomeController {
    @Autowired
    private IDetailService detailService;

    @GetMapping(value="/getData")
    @ResponseBody
    public ResponseResult getData(){
        List<String> currentWeeksDays = DateUtil.getCurrentWeeksDays();
        Map<String,Object> map = new HashMap<>();
        map.put("allDays",currentWeeksDays);
        getMonthsData(map);
        List<Float> listIn = new ArrayList<>();
        List<Float> listOut = new ArrayList<>();
        for (String currentWeeksDay :currentWeeksDays ) {
            List<Map<String,Object>> list = detailService.findHomeTotal(currentWeeksDay);
            if (list == null || list.size() == 0) {
                listIn.add(0f);
                listOut.add(0f);
            }else{
                for (Map<String, Object> stringObjectMap : list) {
                    int type  = (int)stringObjectMap.get("type");
                    if (1 == type){
                        listIn.add(stringObjectMap.get("totalPrice") == null ? 0f :Float.parseFloat(stringObjectMap.get("totalPrice").toString()));
                    }
                    if (2 == type){
                        listOut.add(stringObjectMap.get("totalPrice") == null ? 0f :Float.parseFloat(stringObjectMap.get("totalPrice").toString()));
                    }
                }
            }
        }
        map.put("listIn",listIn);
        map.put("listOut",listOut);
        return ResponseResult.success(map);
    }

    private void getMonthsData(Map<String, Object> map) {
        String nowDate_cn = DateUtil.getNowDate_EN();
        String firstDay = DateUtil.getLastDayOfMonth(nowDate_cn, true);
        String lastDay = DateUtil.getLastDayOfMonth(nowDate_cn, false);
        BigDecimal bigDecimal = detailService.findMonthData("1",firstDay,lastDay);
        BigDecimal bigDecimal2 = detailService.findMonthData("2",firstDay,lastDay);
        map.put("currentMonthDataBuy",bigDecimal);
        map.put("currentMonthDataSell",bigDecimal2);
    }
}
